package cn.breeze.pet.service.impl;

import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.utils.BaiduAuditUtils;
import cn.breeze.pet.domain.Pet;
import cn.breeze.pet.domain.PetDetail;
import cn.breeze.pet.domain.PetOnlineAuditLog;
import cn.breeze.pet.mapper.PetDetailMapper;
import cn.breeze.pet.mapper.PetMapper;
import cn.breeze.pet.mapper.PetOnlineAuditLogMapper;
import cn.breeze.pet.service.IPetService;
import cn.breeze.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author breeze
 * @since 2022-07-16
 */
@Service
// 只读事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void remove(Long id) {
        petMapper.remove(id);
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.save(detail);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        petMapper.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.update(detail);
    }

    @Override
    @Transactional
    public void patchRemove(Long[] ids) {
        petMapper.patchRemove(ids);
        petDetailMapper.patchRemoveByPetId(ids);
    }

    /**
    * @Title: onsale
    * @Description: 商品上架
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/16 23:57
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void onsale(Long[] ids) {
        // 根据ids去查询所有数据
        List<Pet> pets = petMapper.patchLoadById(ids);
        List<Long> list = new ArrayList<>();
        for (Pet pet : pets) {
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setPetId(pet.getId());
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                petOnlineAuditLog.setNote("审核失败，不合规的名字，名字为：" + pet.getName());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                continue;
            }
            String resources = pet.getResources();
            Integer intTmp = 0;
            if (resources != null){
                String[] split = resources.split(",");
                for (String s : split) {
                    if (!BaiduAuditUtils.ImgCensorURL(s)){
                        intTmp = 1;
                        petOnlineAuditLog.setNote("审核失败，不合规的图形，图片地址为：" + s);
                        petOnlineAuditLogMapper.save(petOnlineAuditLog);
                        continue;
                    }
                }
            }
            if (intTmp == 1){
                continue;
            }
            list.add(pet.getId());
        }
        petMapper.onsale(list);
    }

    /**
    * @Title: offsale
    * @Description: 商品下架
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/16 23:57
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void offsale(Long[] ids) {
        petMapper.offsale(ids);
    }
}
